//
//  LogCatLocaltionHelper.m
//  LogCat
//
//  Created by 清风徐来 on 2024/1/8.
//

#import "LogCatLocaltionHelper.h"
#import <CoreLocation/CoreLocation.h>

@interface LogCatLocaltionHelper () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end

@implementation LogCatLocaltionHelper

static LogCatLocaltionHelper *_sharedInstance;
+ (instancetype)shared {
    if(_sharedInstance == nil) {
        _sharedInstance = [[LogCatLocaltionHelper alloc] init];
    }
    return _sharedInstance;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [super allocWithZone:zone];
    });
    return _sharedInstance;
}

- (id)copyWithZone:(NSZone *)zone {
    return _sharedInstance;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _sharedInstance;
}

- (BOOL)isLocationServicesEnabled {
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
        // 定位权限已授予
        return YES;
    } else {
        // 定位权限未授予
        return NO;
    }
}

- (void)startLocation {
    // 创建CLLocationManager对象
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    // 请求定位权限，根据你的需求选择适当的权限类型
    [self.locationManager requestWhenInUseAuthorization]; // 请求使用期间定位权限
    // 启动定位服务
    [self.locationManager startUpdatingLocation];
}


#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *location = [locations lastObject];
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"经度：%f，纬度：%f", coordinate.longitude, coordinate.latitude);

    // 在这里可以使用经纬度信息进行其他操作
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"定位失败：%@", error.localizedDescription);
}


@end
